using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace SPCustomMasterPage.Features.CustomMasterPage
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("b849e4b6-4b17-4411-bb0d-823469cc3dd5")]
    public class CustomMasterPageEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                SPWeb topLevelSite = siteCollection.RootWeb;

                // Calculate relative path to site from Web Application root.
                string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
                if (!WebAppRelativePath.EndsWith("/"))
                {
                    WebAppRelativePath += "/";
                }

                // Enumerate through each site and apply branding.
                foreach (SPWeb site in siteCollection.AllWebs)
                {
                    site.MasterUrl = WebAppRelativePath +
                                     "_catalogs/masterpage/CustomMasterPage.master";
                    site.CustomMasterUrl = WebAppRelativePath +
                                           "_catalogs/masterpage/CustomMasterPage.master";
                    site.AlternateCssUrl = WebAppRelativePath +
                                           "Style Library/CustomTheme/Stylesheet.css";
                    site.SiteLogoUrl = WebAppRelativePath +
                                       "Style Library/CustomTheme/Images/Logo.gif";
                    site.UIVersion = 4;
                    site.Update();
                }

            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                SPWeb topLevelSite = siteCollection.RootWeb;

                // Calculate relative path of site from Web Application root.
                string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
                if (!WebAppRelativePath.EndsWith("/"))
                {
                    WebAppRelativePath += "/";
                }

                // Enumerate through each site and remove custom branding.
                foreach (SPWeb site in siteCollection.AllWebs)
                {
                    site.MasterUrl = WebAppRelativePath +
                                     "_catalogs/masterpage/v4.master";
                    site.CustomMasterUrl = WebAppRelativePath +
                                           "_catalogs/masterpage/v4.master";
                    //site.AlternateCssUrl = "";
                    //site.SiteLogoUrl = "";
                    site.Update();
                }
            }


            // Uncomment the method below to handle the event raised after a feature has been installed.

            //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
            //{
            //}


            // Uncomment the method below to handle the event raised before a feature is uninstalled.

            //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
            //{
            //}

            // Uncomment the method below to handle the event raised when a feature is upgrading.

            //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
            //{
            //}
        }
    }
}
